package net.vinote.smart.platform.service.handler;

import java.sql.SQLException;
import java.util.logging.Level;

import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBTransaction;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.log.RunLogger;
import net.vinote.smart.platform.service.dw.handler.CommonHandler;
import net.vinote.smart.platform.service.dw.handler.HandlerPerimissionAnnotation;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;

/**
 * @author Seer
 * @version LayoutCatalogHandler.java, v 0.1 2015年3月5日 上午11:08:46 Seer Exp.
 */
@HandlerPerimissionAnnotation(
		insert = @Permission(PermissionEnum.SMART_LAYOUT_CATALOG_INSERT),
		update = @Permission(PermissionEnum.SMART_LAYOUT_CATALOG_UPDATE),
		delete = @Permission(PermissionEnum.SMART_LAYOUT_CATALOG_DELETE),
		query = @Permission(PermissionEnum.SMART_LAYOUT_CATALOG_QUERY))
public class LayoutCatalogHandler extends CommonHandler {

	@Override
	public void initDisplayForEdit(BizObject bo) {
		setReadOnly("LayoutID", true);
		setDisabledCSSFields("LayoutStyle,Subject", true);
	}

	@Override
	public void initDisplayForAdd(BizObject bo) {
		String layoutStyle = StringFunction.nullToBlank(request
				.getParameter("layoutStyle"));
		String subject = StringFunction.nullToBlank(request
				.getParameter("subject"));
		bo.setAttribute("LAYOUTSTYLE", layoutStyle);
		bo.setAttribute("subject", subject);
	}

	@Override
	protected void beforeDelete(BizObject bo, DBTransaction tx)
			throws SQLException {
		String layoutID = bo.getAttribute("LAYOUTID");
		DBManager bm = new DBManager("LAYOUT_ITEM");
		tx.join(bm);
		int effectNum = bm.deleteWhereSql("LAYOUTID=?", layoutID);
		RunLogger.getLogger().log(Level.FINE,
				"Delete " + effectNum + " Layout_Item records!");
	}
}
